package xiang.weiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xiang.weiyu.common.BaseContext;
import xiang.weiyu.mapper.ShoppingCartMapper;
import xiang.weiyu.pojo.ShoppingCart;
import xiang.weiyu.service.ShoppingCartService;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    //添加购物车功能
    @Override
    public void insertShoppingCart(ShoppingCart shoppingCart) {
        //从线程副本中获取用户登录ID
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        //套餐不为空,说明添加到购物车的是套餐,反之是菜品
        if (shoppingCart.getSetmealId() != null) {
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            qw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        ShoppingCart one = this.getOne(qw);

        //one==null购物车内不存在:直接保存   反之已经存在:进行修改 点击份数+1
        if (one == null) {
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            this.save(shoppingCart);
        } else {
            one.setNumber(one.getNumber() + 1);
            this.updateById(one);
        }
    }
}
